package controller;

import model.Menu;
import model.Role;
import service.MenuService;
import service.RoleService;
import service.impl.MenuServiceImpl;
import service.impl.RoleServiceImpl;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/power/role/roleServlet")
public class RoleServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();
    MenuService menuService = new MenuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("selectRole".equals(method)) {
            selectRole(req, resp);
        } else if ("select".equals(method)) {
            select(req, resp);
        } else if ("selectmenus".equals(method)) {
            selectmenus(req, resp);
        } else if ("insert".equals(method)) {
            insert(req, resp);
        } else if ("beforeUpdate".equals(method)) {
            beforeUpdate(req, resp);
        } else if ("update".equals(method)) {
            update(req, resp);
        } else if ("delete".equals(method)) {
            delete(req, resp);
        }
    }

    //新增用户，修改用户时，用于动态查询数据库内角色信息
    protected void selectRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> allRole = roleService.getRole();
        req.setAttribute("allRole", allRole);
        req.getRequestDispatcher("/power/user/add.jsp").forward(req, resp);
    }

    //查询角色信息时的分页查
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageUtil pageUtil = new PageUtil();
        String index = req.getParameter("index");
        int pageIndex = (index == null || index.length() == 0) ? 1 : Integer.parseInt(index);
        List<Role> allRole = roleService.getAllRole(pageIndex, pageUtil.getPageSize());
        req.setAttribute("allRole", allRole);
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDateList(allRole);

        //3. 存值跳转
        req.setAttribute("pi", pageUtil);
        req.getRequestDispatcher("/power/role/list.jsp").forward(req, resp);
    }

    //查询菜单列表
    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接收参数
        //2. 调用service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList", menuList);
        req.getRequestDispatcher("add.jsp").forward(req, resp);
    }

    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String contextPath = req.getContextPath();
        //1. 接收参数
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        //2. 调用service方法
        int i = roleService.insertRole(rolename, state, menuids);

        if (i > 0) {
            writer.println("<script>alert('新增成功');location.href='" + contextPath + "/power/role/roleServlet?method=select'</script>");
        } else {
            writer.println("<script>alert('新增失败');location.href='" + contextPath + "/power/role/roleServlet?method=selectmenus'</script>");
        }
    }

    //在修改前，先进行主键查询
    protected void beforeUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 查询
        String roleid = req.getParameter("roleid");
        String flag = req.getParameter("flag");
        Role roleById = roleService.getRoleById(Integer.parseInt(roleid));
        req.setAttribute("roleById", roleById);
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList", menuList);
        if (flag != null && flag.length() > 0)
            req.getRequestDispatcher("info.jsp").forward(req, resp);
        else
            req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }

    //正式进行修改
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String contextPath = req.getContextPath();
        //1. 接收参数
        String roleid = req.getParameter("roleid");
        String rolename = req.getParameter("rolename");
        String[] menus = req.getParameterValues("menu");
        String state = req.getParameter("state");
        //2. 调用service方法
        int update = roleService.update(Integer.parseInt(roleid), rolename, state, menus);
        if (update > 0) {
            writer.println("<script>alert('修改成功');location.href='" + contextPath + "/power/role/roleServlet?method=select'</script>");
        } else {
            writer.println("<script>alert('修改失败');location.href='" + contextPath + "/power/role/roleServlet?method=select'</script>");
        }
    }

    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String contextPath = req.getContextPath();
        //1. 接收参数
        String roleid = req.getParameter("roleid");
        //2. 调用service方法
        int update = roleService.delete(Integer.parseInt(roleid));
        if (update > 0) {
            writer.println("<script>alert('删除成功');location.href='" + contextPath + "/power/role/roleServlet?method=select'</script>");
        } else {
            writer.println("<script>alert('删除失败');location.href='" + contextPath + "/power/role/roleServlet?method=select'</script>");
        }
    }
}
